package com.wxzz.elearing.course.api.admin;

import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.course.dto.StudyPlanDto;
import com.wxzz.elearing.course.dto.StudyPlanInsDto;
import com.wxzz.elearing.course.dto.WxUserStudyPlanDto;
import com.wxzz.elearing.course.dto.WxUserStudyPlanInsDto;
import com.wxzz.elearing.course.entity.Curriculum;
import com.wxzz.elearing.course.entity.StudyPlan;
import com.wxzz.elearing.course.entity.StudyPlanUnit;
import com.wxzz.elearing.course.entity.WxUserStudyPlan;
import com.wxzz.elearing.course.service.IStudyPlanService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/study-plan")
@Api(tags = "学习计划(后台)")
public class StudyPlanController {

    @Autowired
    private IStudyPlanService studyPlanService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "添加学习计划", notes = "添加学习计划", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveStudyPlan", method = RequestMethod.POST)
    public ReturnVO saveStudyPlan(@RequestHeader String token, @RequestBody StudyPlanInsDto studyPlanInsDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return studyPlanService.saveStudyPlan(jsonObject.getLong("enterpriseId"), studyPlanInsDto);
    }

    @ApiOperation(value = "删除学习计划", notes = "删除学习计划", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "deleteStudyPlanById/{studyPlanById}", method = RequestMethod.POST)
    public ReturnVO deleteStudyPlanById(@RequestHeader String token, @PathVariable Long studyPlanById) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return studyPlanService.deleteStudyPlanById(studyPlanById);
    }

    @ApiOperation(value = "学习计划列表", notes = "学习计划列表", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listStudyPlan", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<StudyPlan>>> listStudyPlan(@RequestHeader String token, @RequestBody StudyPlanDto studyPlanDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return studyPlanService.listStudyPlan(jsonObject.getLong("enterpriseId"), studyPlanDto);
    }

    @ApiOperation(value = "根据学习计划id列出学习单元", notes = "根据学习计划id列出学习单元", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "listStudyPlanUnitByStudyPlanId/{studyPlanId}", method = RequestMethod.POST)
    public ReturnVO<List<StudyPlanUnit>> listStudyPlanUnitByStudyPlanId(@PathVariable Long studyPlanId) {
        return studyPlanService.listStudyPlanUnitByStudyPlanId(studyPlanId);
    }

    @ApiOperation(value = "查询学习计划详情", notes = "查询学习计划详情", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectStudyPlanDetail/{studyPlanId}", method = RequestMethod.POST)
    public ReturnVO<StudyPlanInsDto> selectStudyPlanDetail(@PathVariable Long studyPlanId) {
        return studyPlanService.selectStudyPlanDetail(null,studyPlanId);
    }


//    @ApiOperation(value = "根据学习计划id与用户id查询对应课程", notes = "根据学习计划id与用户id查询对应课程", httpMethod = "POST", tags = "后台")
//    @RequestMapping(value = "listCurriculumByStudyPlanId/{wxUserId}/{studyPlanId}", method = RequestMethod.POST)
//    public ReturnVO<List<StudyPlanUnit>> listCurriculumByStudyPlanId(@PathVariable Long wxUserId,@PathVariable Long studyPlanId) {
//        return studyPlanService.listCurriculumByStudyPlanId(wxUserId,studyPlanId);
//    }

    @ApiOperation(value = "根据学习计划id与用户id查询详情", notes = "根据学习计划id与用户id查询对应课程", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectStudyPlanDetail/{wxUserId}/{studyPlanId}", method = RequestMethod.POST)
    public ReturnVO<StudyPlanInsDto> selectStudyPlanDetail(@PathVariable Long wxUserId,@PathVariable Long studyPlanId) {
        return studyPlanService.selectStudyPlanDetail(wxUserId,studyPlanId);
    }

    @ApiOperation(value = "根据学习计划id查询学习计划人员", notes = "根据学习计划id查询学习计划人员", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "selectWxUserStudyPlanByStudyPlanId", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<WxUserStudyPlan>>> selectWxUserStudyPlanByStudyPlanId(@RequestBody WxUserStudyPlanDto wxUserStudyPlanDto) {
        return studyPlanService.selectWxUserStudyPlanByStudyPlanId(wxUserStudyPlanDto);
    }

    @ApiOperation(value = "分配学习计划人员", notes = "分配学习计划人员", httpMethod = "POST", tags = "后台")
    @RequestMapping(value = "saveWxUserStudyPlan", method = RequestMethod.POST)
    public ReturnVO<StudyPlanInsDto> saveWxUserStudyPlan(@RequestBody WxUserStudyPlanInsDto wxUserStudyPlanInsDto) {
        return studyPlanService.saveWxUserStudyPlan(wxUserStudyPlanInsDto);
    }


    @ApiOperation(value = "查询该用户学习计划(后台)", notes = "查询该用户学习计划(后台)", httpMethod = "POST",tags = "后台")
    @RequestMapping(value = "wxListStudyPlan", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<StudyPlan>>> wxListStudyPlan( @RequestBody StudyPlanDto studyPlanDto) {
        return studyPlanService.wxListStudyPlan(studyPlanDto.getWxUserId(), studyPlanDto.getEnterpriseId(), studyPlanDto);
    }

//    @ApiOperation(value = "新增或修改学习单元", notes = "新增或修改学习单元", httpMethod = "POST",tags = "后台")
//    @RequestMapping(value = "saveStudyPlanUnit", method = RequestMethod.POST)
//    public ReturnVO saveStudyPlanUnit(@RequestHeader String token, @RequestBody StudyPlanUnit studyPlanUnit) {
//        return studyPlanService.saveStudyPlanUnit(studyPlanUnit);
//    }

}
